<template>
<div>
  <el-tabs v-model="activeName" >
    <el-tab-pane label="仓库管理" align="center" name="first">
      <h1>仓库管理</h1>
      <el-form :inline="true" :model="keyword" class="demo-form-inline" ref="searchForm">
        <el-form-item label="联系人" prop="supGys">
          <el-input size="small" clearable v-model="keyword.depName" placeholder="联系人"></el-input>
        </el-form-item>
        <el-form-item label="仓库名称" prop="cliKh">
          <el-input size="small" clearable v-model="keyword.depCk" placeholder="仓库名称"></el-input>
        </el-form-item>
        <el-button type="primary" size="small"  @click="search" icon="el-icon-search">查询</el-button>
        <el-button size="small" icon="el-icon-refresh" @click="resetsearchForm('searchForm')">重置</el-button>
        <span style="margin-left: 600px">
              <el-button type="primary" @click="openAddDialog" icon="el-icon-plus" >添加</el-button>
              <el-button type="danger" @click="deletebatches" >批量删除</el-button  >
                </span>
      </el-form>
      <el-table v-loading="loading" :data="depotList" @selection-change="handleSelectionChange" :row-class-name="tableRowClassName">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="仓库编号" align="center" prop="depId"/>
        <el-table-column label="仓库名称" align="center" prop="depCk" >
        </el-table-column>
        <el-table-column label="仓库描述" align="center" prop="depDescribe"/>
        <el-table-column label="联系人" align="center" prop="depName"/>
        <el-table-column label="联系电话" align="center" prop="depPhone" />
        <el-table-column label="联系地址" align="center" prop="depHome" />
        <el-table-column label="创建人" align="center" prop="depCreator" />
        <el-table-column label="创建时间" align="center" prop="depTime" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleEdit(scope.row)">更新</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5, 8, 10]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-tab-pane>
    <el-tab-pane label="添加仓库"  name="second">
      <h1 align="center" style="color:firebrick">添加仓库</h1>
      <el-form  :model="depot" label-width="150px"  :rules="clientRules" ref="itemTypeRulesForm">
        <el-form-item label="仓库名称：" prop="depCk">
          <el-input v-model="depot.depCk" style="width: 500px" placeholder="仓库名称"></el-input>
        </el-form-item>
        <el-form-item label="描述：" >
        <el-input
          type="textarea"
          :rows="2"
          style="width: 500px"
          placeholder="请输入内容"
          v-model="depot.depDescribe">
        </el-input>
        </el-form-item>
        <el-form-item label="联系人：" prop="depName">
          <el-input v-model="depot.depName" style="width: 500px" placeholder="联系人"></el-input>
        </el-form-item>
        <el-form-item label="手机号：" prop="depPhone">
          <el-input v-model="depot.depPhone" style="width: 500px" placeholder="手机号"></el-input>
        </el-form-item>
        <el-form-item label="详细地址">
          <el-select v-model="a" placeholder="请选择所属区域">
            <el-option label="河南省" value="河南省"></el-option>
            <el-option label="广东省" value="广东省"></el-option>
          </el-select>
          <el-select v-model="b" placeholder="请选择所属区域">
            <el-option label="郑州" value="郑州"></el-option>
            <el-option label="深圳" value="深圳"></el-option>
          </el-select>
          <el-select v-model="c" placeholder="请选择所属区域">
            <el-option label="金水区" value="金水区"></el-option>
            <el-option label="龙岗区" value="龙港区"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="详细地址：">
          <el-input v-model="depot.depHome"  placeholder="详细地址"></el-input>
        </el-form-item>
        <el-form-item label="创建人：">
          <el-input v-model="depot.depCreator"  placeholder="创建人"></el-input>
        </el-form-item>
        <el-form-item label="创建日期：">
        <el-col :span="11">
          <el-date-picker type="date" placeholder="选择日期" v-model="depot.depTime" style="width: 100%;"></el-date-picker>
        </el-col>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit('itemTypeRulesForm')">立即添加</el-button>
          <el-button type="primary" @click="onlot">取消</el-button>
        </el-form-item>
      </el-form>
    </el-tab-pane>
    <el-tab-pane label="更新仓库"  name="up"  :disabled="xianshi">
      <h1 align="center" style="color:firebrick" >更新仓库</h1>
      <el-form  :model="depot" label-width="150px"  :rules="clientRules1" ref="itemTypeRulesForm">
        <el-form-item label="仓库名称：" prop="depCk">
          <el-input v-model="depot.depCk" style="width: 500px" placeholder="仓库名称"></el-input>
        </el-form-item>
        <el-form-item label="描述：" >
          <el-input
            type="textarea"
            :rows="2"
            style="width: 500px"
            placeholder="请输入内容"
            v-model="depot.depDescribe">
          </el-input>
        </el-form-item>
        <el-form-item label="联系人：" prop="depName">
          <el-input v-model="depot.depName" style="width: 500px" placeholder="联系人"></el-input>
        </el-form-item>
        <el-form-item label="手机号：" prop="depPhone">
          <el-input v-model="depot.depPhone" style="width: 500px" placeholder="手机号"></el-input>
        </el-form-item>
        <el-form-item label="详细地址">
          <el-select v-model="a" placeholder="请选择所属区域">
            <el-option label="河南省" value="河南省"></el-option>
            <el-option label="广东省" value="广东省"></el-option>
          </el-select>
          <el-select v-model="b" placeholder="请选择所属区域">
            <el-option label="郑州" value="郑州"></el-option>
            <el-option label="深圳" value="深圳"></el-option>
          </el-select>
          <el-select v-model="c" placeholder="请选择所属区域">
            <el-option label="金水区" value="金水区"></el-option>
            <el-option label="龙岗区" value="龙港区"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="详细地址：">
          <el-input v-model="depot.depHome"  placeholder="详细地址"></el-input>
        </el-form-item>
        <el-form-item label="创建人：">
          <el-input v-model="depot.depCreator"  placeholder="创建人"></el-input>
        </el-form-item>
        <el-form-item label="创建日期：">
          <el-col :span="11">
            <el-date-picker type="date" placeholder="选择日期" v-model="depot.depTime" style="width: 100%;"></el-date-picker>
          </el-col>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit1('itemTypeRulesForm')">立即更新</el-button>
          <el-button type="primary" @click="onlot1">取消</el-button>
        </el-form-item>
      </el-form>
    </el-tab-pane>

  </el-tabs>

</div>
</template>
<script>
  import {getList1,getdel,add1,delBatch,up1} from '@/api/depot'
  export default {
  data(){
  return{
    xianshi:true,
    a:null,
    b:null,
    c:null,
    activeName:'first',
    clientRules:{
      depCk:[
        { required: true, message: '客户名称不能为空', trigger: 'blur' },
      ],
      depName:[
        { required: true, message: '联系人不能为空', trigger: 'blur' },
      ],
      depPhone:[
        { required: true, message: '联系人手机号不能为空', trigger: 'blur' },
      ],
    },
    clientRules1:{
      depCk:[
        { required: true, message: '客户名称不能为空', trigger: 'blur' },
      ],
      depName:[
        { required: true, message: '联系人不能为空', trigger: 'blur' },
      ],
      depPhone:[
        { required: true, message: '联系人手机号不能为空', trigger: 'blur' },
      ],
    },
    depot:{
      depDescribe:null,
      depCk:null,
      depName:null,
      depPhone:null,
      depHome:null,
      depCreator:null,
      depTime:null
    },
    tabPosition: 'top',
  loading:true,
    depotList:[],
    pageNum:1,
    pageSize:5,
    currentPage:1,
    total:0,
    keyword: {
    depName: null,
    depCk: null,
}
}
},
  methods:{
    tableRowClassName({row, rowIndex}) {
      if (rowIndex === 1) {
        return 'warning-row';
      } else if (rowIndex === 3) {
        return 'success-row';
      }else if (rowIndex === 5){
        return 'warning-row';
      }else if (rowIndex === 7){
        return 'success-row';
      }
      return '';
    },
  fetchData(){
  let params = {
  pageNum: this.pageNum,
  pageSize: this.pageSize,
    depName: this.keyword.depName,
    depCk: this.keyword.depCk
}
  getList1(params).then(res=>{
  let pageInfo = res.data;
  this.loading = false;
  this.depotList = res.data;
  this.depotList = pageInfo.list;
  this.total = pageInfo.total
})
},
  //多选框
  handleSelectionChange(selectionRowArr){
    this.ids=[];
    for (let i = 0; i < selectionRowArr.length; i++) {
      this.ids.push(selectionRowArr[i].depId)
    }
},
  //分页
  handleSizeChange(val){
    this.pageSize = val; // 将当前页大小存起来，方便下一方法使用
    console.log(`每页 ${val} 条`);
    this.fetchData()
},
  //分页
  handleCurrentChange(val){
    this.pageNum = val;
    console.log(`当前页: ${val}`);
    this.fetchData()
},
  //重置按钮
  resetsearchForm(formName){
    this.$refs[formName].resetFields();
    this.keyword.depCk=null
    this.keyword.depName=null
    this.fetchData()
},
  //更新按钮
  handleEdit(row){
    this.activeName='up'
    this.depot=row
},
    //修改提交按钮
    onSubmit1(formName){
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.depot.depHome = this.a + this.b + this.c+this.depot.depHome
          up1(this.depot).then((res) => {
            this.$message({
              type: 'success',
              message: '更新成功',
              duration: 1000,
            });
            this.fetchData()
            this.depot = {}
            this.a = null
            this.b = null
            this.c = null
            this.activeName = 'first'
          })
        }else{
          this.$message({
            type: 'success',
            message: '更新失败',
            duration: 1000, })
        }
      })
    },
    //添加按钮
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.depot.depHome = this.a + this.b + this.c+this.depot.depHome
          add1(this.depot).then((res) => {
            this.$message({
              type: 'success',
              message: '添加成功',
              duration: 1000,
            });
            this.fetchData()
            this.depot = {}
            this.a = null
            this.b = null
            this.c = null
            this.d = null
            this.activeName = 'first'
          })
        }else{
          this.$message({
            type: 'success',
            message: '添加失败',
            duration: 1000, })
        }
      })
    },
    onlot(){
      this.fetchData()
      this.depot = {}
      this.a=null
      this.b=null
      this.c=null
      this.activeName='first'
    },
    onlot1(){
      this.fetchData()
      this.depot = {}
      this.a=null
      this.b=null
      this.c=null
      this.activeName='first'
    },
  //单个删除按钮
    handleDelete(row){
      this.$confirm('此操作将永久删除编号为['+row.depId+']的数据?', { //this.idList这是前端多选框中选的数据
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        let params={
          depId:row.depId
        }
        getdel(params
        ).then(res=>{
          this.$message({
            type: 'success',
            message: '删除成功!',
            duration:1000
          });
          this.fetchData()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除',
          duration:1000
        });
      });
    },
  //添加按钮
  openAddDialog(){
      this.activeName = 'second'
    },
    deletebatches(){
      this.$confirm('此操作将永久删除编号为['+this.ids+']的数据?', { //this.idList这是前端多选框中选的数据
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        let idStr = this.ids.join(",")
        let params={
          ids:idStr
        }
        delBatch(params
        ).then(res=>{
          this.$message({
            type: 'success',
            message: '删除成功!',
            duration:1000
          });
          this.fetchData()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除',
          duration:1000
        });
      });

    },
  //查询按钮
  search(){
    this.fetchData()
}
},
  created() {
  this.fetchData()
},
}
</script>
<style>
.el-table .warning-row {
  background: oldlace;
}

.el-table .success-row {
  background: #f0f9eb;
}
</style>
